package com.sg.java.apps;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Test3 {

    public static void main(String[] args) throws Exception {
//        List<String> rawList = new ArrayList<>();
//        String       line    = "dasda,\"dsdsd,123123,gttg\",我我,\"哈哈哈,午餐\",实时";
//
//        int          quote   = 0;
//        final char[] chars    = line.toCharArray();
//        int          subIndex = 0;
//        for (int i = 0; i < chars.length; i++) {
//            char c = chars[i];
//            if (c == '\"') {
//                quote++;
//            }
////            System.out.println(c + "--------------" + quote);
//            if (c == ',') {
//                //双数引号，则此逗号可作为分隔符
//                if (quote % 2 == 0) {
//                    rawList.add(line.substring(subIndex, i));
//                    subIndex = i + 1;
////                    System.out.println(rawList + "--------------" + line);
//                }
//            }
//        }
//        rawList.add(line.substring(subIndex));
//        String[] raw = rawList.toArray(new String[0]);
//        System.out.println(Arrays.toString(raw));

        test1();
    }

    private static void test1() {
        String                                   path               = "C:\\Users\\HUAWEI\\Desktop\\studyofjava17\\target\\studyofjava17-1.0-SNAPSHOT.jar";//外部jar包的路径
        Set<Class<?>>                            classes            = new LinkedHashSet<>();//所有的Class对象
        Map<Class<?>, Annotation[]>              classAnnotationMap = new HashMap<>();//每个Class对象上的注释对象
        Map<Class<?>, Map<Method, Annotation[]>> classMethodAnnoMap = new HashMap<>();//每个Class对象中每个方法上的注释对象
        try {
            JarFile               jarFile = new JarFile(new File(path));
            URL                   url     = new URL("file:" + path);
            ClassLoader           loader  = new URLClassLoader(new URL[]{url});//自己定义的classLoader类，把外部路径也加到load路径里，使系统去该路经load对象
            Enumeration<JarEntry> es      = jarFile.entries();
            while (es.hasMoreElements()) {
                JarEntry jarEntry = (JarEntry) es.nextElement();
                String   name     = jarEntry.getName();
                if (name != null && name.endsWith(".class")) {//只解析了.class文件，没有解析里面的jar包
                    //默认去系统已经定义的路径查找对象，针对外部jar包不能用
                    //Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(name.replace("/", ".").substring(0,name.length() - 6));
                    Class<?> c = loader.loadClass(name.replace("/", ".")
                                                      .substring(0, name.length() - 6));//自己定义的loader路径可以找到
                    System.out.println(c);
                    classes.add(c);
                    Annotation[] classAnnos = c.getDeclaredAnnotations();
                    classAnnotationMap.put(c, classAnnos);
                    Method[]                  classMethods  = c.getDeclaredMethods();
                    Map<Method, Annotation[]> methodAnnoMap = new HashMap<Method, Annotation[]>();
                    for (int i = 0; i < classMethods.length; i++) {
                        Annotation[] a = classMethods[i].getDeclaredAnnotations();
                        methodAnnoMap.put(classMethods[i], a);
                    }
                    classMethodAnnoMap.put(c, methodAnnoMap);
                }
            }
            System.out.println(classes.size());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
